<template>
  <!-- 顶部导航栏 -->
  <div class="header">
    <div class="header-content">
      <span class="logo">
        <span class="logo-icon">📊</span>
        <span class="logo-text">LearnShare</span>
      </span>
      <el-dropdown>
        <span class="user" v-if="isLoggedIn">
          <img :src="user_pic" alt="头像" class="user-pic" />
          <span class="username">{{ username }}</span>
          <el-icon class="user-arrow"><arrow-down /></el-icon>
        </span>
        <el-button class="login" v-else @click="login">
          <el-icon><user-filled /></el-icon>
          <span>登录</span>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu v-if="isLoggedIn" class="user-dropdown">
            <el-dropdown-item class="dropdown-item">
              <el-icon><user /></el-icon>
              <span>个人信息</span>
            </el-dropdown-item>
            <el-dropdown-item class="dropdown-item">
              <el-icon><edit /></el-icon>
              <span>注册</span>
            </el-dropdown-item>
            <el-dropdown-item @click="logout" class="dropdown-item">
              <el-icon><switch-button /></el-icon>
              <span>退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <div class="header-decoration"></div>
  </div>

  <!-- 侧边栏折叠按钮 -->
  <div class="collapse-btn" @click="toggleCollapse">
    <div class="collapse-icon-wrapper">
      <el-icon :title="isCollapse ? '展开菜单' : '折叠菜单'">
        <expand v-if="isCollapse" />
        <fold v-else />
      </el-icon>
    </div>
  </div>

  <!-- 侧边菜单 -->
  <el-menu
      default-active="1"
      class="side-menu"
      :collapse="isCollapse"
      @open="handleOpen"
      @close="handleClose"
      background-color="transparent"
      text-color="black"
      active-text-color="#67c23a"
  >
    <div class="menu-header" v-if="!isCollapse">
      <span>导航菜单</span>
    </div>
    <el-menu-item index="1" class="menu-item">
      <el-icon class="menu-icon"><home-filled /></el-icon>
      <template #title>
        <router-link to="/admin/homeAdmin" class="menu-link">首页</router-link>
      </template>
    </el-menu-item>

    <el-sub-menu index="2" >
      <template #title>
        <el-icon><UserFilled /></el-icon>
        <span>用户管理</span>
      </template>
      <el-menu-item-group >
        <router-link to="/admin/userManage" class="menu-link"><el-menu-item index="2-1">用户列表</el-menu-item></router-link>
        <router-link to="/admin/pwdReset" class="menu-link"><el-menu-item index="2-2">密码重置</el-menu-item></router-link>
      </el-menu-item-group>
    </el-sub-menu>

    <el-sub-menu index="3">
      <template #title>
        <el-icon><Document /></el-icon>
        <span>文章管理</span>
      </template>
      <el-menu-item-group>
        <router-link to="/admin/articleManage" class="menu-link"><el-menu-item index="3-1">文章管理</el-menu-item></router-link>
        <router-link to="/admin/applicationManage" class="menu-link"><el-menu-item index="3-2">申请审批</el-menu-item></router-link>
      </el-menu-item-group>
    </el-sub-menu>

    <el-sub-menu index="4">
      <template #title>
        <el-icon><Setting /></el-icon>
        <span>类型管理</span>
      </template>
      <el-menu-item-group>
        <router-link to="/admin/categoryManage" class="menu-link"><el-menu-item index="4-1">分类管理</el-menu-item></router-link>
      </el-menu-item-group>
    </el-sub-menu>
    <el-sub-menu index="5">
      <template #title>
        <el-icon><Setting /></el-icon>
        <span>评论管理</span>
      </template>
      <el-menu-item-group>
        <router-link to="/admin/commentManage" class="menu-link"><el-menu-item index="5-1">用户评论管理</el-menu-item></router-link>
      </el-menu-item-group>
    </el-sub-menu>
    <el-sub-menu index="6">
      <template #title>
        <el-icon><Setting /></el-icon>
        <span>模型管理</span>
      </template>
      <el-menu-item-group>
        <router-link to="/admin/refresh-model" class="menu-link">
          <el-menu-item index="6-1">刷新推荐模型</el-menu-item>
        </router-link>
      </el-menu-item-group>
    </el-sub-menu>
  </el-menu>

  <!-- 主内容区 -->
  <div class="main-content" :style="{ marginLeft: marginLeftValue }">
    <router-view />
  </div>
</template>

<style scoped>
/* 基础变量 */
:root {
  --primary-color: #3a8ee6;
  --secondary-color: #67c23a;
  --dark-bg: #1a2b3c;
  --light-bg: #f5f7fa;
  --text-dark: #303133;
  --text-light: #ffffff;
  --border-color: #e6e6e6;
  --shadow-color: rgba(0, 0, 0, 0.1);
  --transition-fast: all 0.3s ease;
  --transition-slow: all 0.6s cubic-bezier(0.25, 0.8, 0.25, 1);
}

/* 顶部导航栏 */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: linear-gradient(135deg, var(--dark-bg) 0%, #2c3e50 100%);
  color: var(--text-light);
  z-index: 1000;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 30px;
  position: relative;
}

.header-decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--primary-color), transparent);
  opacity: 0.7;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  transition: var(--transition-fast);
}

.logo-icon {
  margin-right: 10px;
  font-size: 24px;
}

.logo-text {
  position: relative;
}

.logo-text::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 0;
  height: 2px;
  background: var(--secondary-color);
  transition: var(--transition-fast);
}

.logo:hover .logo-text::after {
  width: 100%;
}

/* 用户信息 */
.user {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 20px;
  transition: var(--transition-fast);
  background: rgba(255, 255, 255, 0.1);
}

.user:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.user-pic {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: var(--transition-fast);
}

.user:hover .user-pic {
  border-color: var(--secondary-color);
  transform: scale(1.05);
}

.username {
  margin-right: 5px;
  font-size: 14px;
}

.user-arrow {
  margin-left: 5px;
  transition: var(--transition-fast);
}

.user:hover .user-arrow {
  transform: translateY(2px);
}

.user-dropdown {
  border: none;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  background: rgba(30, 40, 50, 0.9);
  backdrop-filter: blur(10px);
}

.dropdown-item {
  color: var(--text-light);
  padding: 10px 20px;
  transition: var(--transition-fast);
}

.dropdown-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--secondary-color);
}

/* 登录按钮 */
.login {
  color: var(--text-light) !important;
  background: rgba(255, 255, 255, 0.1) !important;
  border: none !important;
  border-radius: 20px !important;
  padding: 8px 15px !important;
  transition: var(--transition-fast) !important;
  display: flex;
  align-items: center;
}

.login:hover {
  background: rgba(255, 255, 255, 0.2) !important;
  transform: translateY(-2px) !important;
}

.login i {
  margin-right: 5px;
}

/* 折叠按钮 */
.collapse-btn {
  position: fixed;
  top: 70px;
  left: 10px;
  z-index: 999;
  cursor: pointer;
  transition: var(--transition-slow);
}

.collapse-icon-wrapper {
  width: 36px;
  height: 36px;
  background: var(--dark-bg);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  transition: var(--transition-fast);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.collapse-icon-wrapper:hover {
  background: var(--primary-color);
  transform: scale(1.1);
  box-shadow: 0 5px 15px rgba(58, 142, 230, 0.4);
}

.collapse-icon-wrapper i {
  color: var(--text-light);
  font-size: 18px;
}

/* 侧边菜单 */
.side-menu {
  position: fixed;
  top: 60px;
  left: 0;
  bottom: 0;
  width: 220px;
  height: calc(100vh - 60px);
  border-right: none;
  transition: var(--transition-slow);
  z-index: 998;
  overflow: hidden;
  background: linear-gradient(to bottom, var(--dark-bg), #2c3e50);
}

.menu-header {
  padding: 20px 20px 15px;
  color: var(--text-light);
  font-size: 16px;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 10px;
}

.menu-item {
  margin: 5px 10px;
  border-radius: 6px;
  transition: var(--transition-fast);
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.05) !important;
}

.menu-item.is-active {
  background: rgba(58, 142, 230, 0.2) !important;
}

.menu-icon {
  margin-right: 8px;
  font-size: 18px;
}

.menu-link {
  color: inherit;
  text-decoration: none;
  display: block;
  width: 100%;
  height: 100%;
}

.el-menu--collapse {
  width: 64px;
}

.el-menu--collapse .menu-icon {
  margin-right: 0;
  font-size: 20px;
}

/* 主内容区 */
.main-content {
  margin-top: 60px;
  margin-left: 220px;
  padding: 20px;
  height: calc(100vh - 110px);
  background: var(--light-bg);
  transition: var(--transition-slow);
  position: relative;
}

.el-menu--collapse + .main-content {
  margin-left: 64px;
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.main-content > * {
  animation: fadeIn 0.5s ease forwards;
}
</style>




<script lang="ts" setup>
import { ref } from 'vue'

import {
  Document,
  HomeFilled,
  Setting,
  Expand,
  Fold,
  UserFilled,
  User,
  Edit,
  SwitchButton
} from '@element-plus/icons-vue'


import router from "@/router";
const isLoggedIn = ref(false); // 使用 ref 创建响应式变量
const user_pic = ref('');
const username = ref('');
// 解析 token
import { getToken, parseToken } from '@/api/tokenDecode';
import {ElMessage, translate} from "element-plus";
import request from "@/api/request";


const getUsernameFromToken = () => {
  const token = getToken(); // 获取 token
  const decodedToken = parseToken(token); // 解析 token
  if (decodedToken && decodedToken.claims) {
    const username = decodedToken.claims.username; // 获取 username
    const user_pic = decodedToken.claims.user_pic; // 获取 userPic
    if (username) {
      return { username, user_pic }; // 返回包含 username 和 userPic 的对象
    } else {
      console.log('没有有效的 Token 或 username 不存在');
      return null; // 如果没有有效的 Token 或 username 不存在，返回 null
    }
  } else {
    console.log('没有有效的 Token 或 claims 不存在');
    return null; // 如果没有有效的 Token 或 claims 不存在，返回 null
  }
};

// 判断用户名
const userInfo = getUsernameFromToken();
if (userInfo) {
  isLoggedIn.value = true; // 更新响应式变量
  username.value = userInfo.username;
  user_pic.value = userInfo.user_pic;
} else {
  console.log('无法获取用户名');
}

const logout =  () => {
  localStorage.removeItem('token')
  localStorage.removeItem('tokenStartTime')
  localStorage.removeItem('userInfo')
  ElMessage.success('登出成功')
  setTimeout(function () {
    router.push('/login');
  }, 1000)
  // 发送登出请求
/*  request.post(`/admin/logout`).then(() => {
    // 清除本地存储的用户信息或令牌
    localStorage.clear(); // 清除所有本地存储数据
    // 跳转到登录页面
    ElMessage.success('登出成功')
    setTimeout(function () {
      router.push('/login');
    }, 1000)
  }).catch((error: any) => {
    console.error("登出失败:", error);
  });*/
};

const login = () => {
  router.push('/login')
}

const isCollapse = ref(false)
const marginLeftValue = ref('220px'); // 默认 margin-left
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
  // 根据 collapse 状态设置 margin-left
  marginLeftValue.value = isCollapse.value ? '80px' : '220px';
}

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

</script>